package com.example.common.utils;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.example.common.R;

/**
 * 时间计数器
 */
public class TimeCount extends CountDownTimer {

    public interface Finished {

        void onFinished();
    }

    private static final long MILLIS_IN_FUTURE = 60 * 1000, COUNT_DOWN_INTERVAL = 1000;

    private Finished mOnFinished;
    private TextView mTextView;

    public TimeCount(TextView view, Finished onFinished) {
        super(MILLIS_IN_FUTURE, COUNT_DOWN_INTERVAL);
        this.mTextView = view;
        this.mOnFinished = onFinished;
    }

    public TimeCount(TextView view) {
        super(MILLIS_IN_FUTURE, COUNT_DOWN_INTERVAL);
        mTextView = view;
    }

    @Override
    public void onFinish() {
        mTextView.setClickable(true);
        mTextView.setText(R.string.click_resend);
        mTextView.setTextColor(Color.parseColor("#3F82DD"));
        if (mOnFinished != null) {
            mOnFinished.onFinished();
        }
    }

    @SuppressLint("StringFormatMatches")
    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false);
        mTextView.setTextColor(Color.GRAY);
        mTextView.setText(String.valueOf(millisUntilFinished / COUNT_DOWN_INTERVAL));
    }
}
